const app = getApp()
import { comment } from '../../../api/goods'

Page({
  data: {
    back: true,
    title: '用户评价',
    loading: true,
    loadmore: false,
    list: [],
    form: {
      page: 1,
      limit: 10,
      goods_id: null
    },
    abnor: {
      title: '还没有用户评价~',
      button: ''
    }
  },
  onLoad (options) {
    this.data.form.goods_id = options.id
    this.setData({ loading: true })
    this.getList()
  },
  onPullDownRefresh () { /* 监听用户下拉动作*/ },
  onReachBottom () {
    if (this.data.form.page == this.data.form.last_page) {
      this.setData({ loadmore: true })
    } else {
      this.data.form.page += 1
      this.getList()
    }
  },

  // ===================methon================

  getList (res) {
    comment(this.data.form).then(res => {
      const { data } = res
      this.data.list || (this.data.list = [])
      const list = this.data.list.concat(data.data)
      this.data.form.last_page = res.data.last_page
      this.setData({
        list: list,
        loadmore: data.last_page <= 1
      })
    })
      .finally(() => {
        this.setData({ loading: false })
        wx.hideLoading()
      })
  },

  previewImage (event) {
    const current = event.currentTarget.dataset.url
    const items = event.currentTarget.dataset.item

    const images = []
    items.forEach(item => {
      images.push(item.image_url)
    })
    wx.previewImage({
      current: current,
      urls: images
    })
  }
})